package com.football;

import data.FootBallClubConstants;
import data.FootBallMemberConstants;
import data.SalaryBase;
import model.FootBallMember;
import model.MostFooterEnum;
import model.Position;

import java.awt.event.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import javax.swing.*;

/**
 * 修改球员信息
 */
public class UpdateMemberFrame extends JDialog {
    private FootBallMember footBallMember;

 // 提示信息
    private JLabel jLabel1;

    private JLabel referNameLable;

    // 姓名label
    private JLabel nameLable;

    // 年龄
    private JLabel ageLabel;

    // 年龄域
    private JTextField ageTextField;

    // 确定添加按钮
    private JButton okBtn;

    // 返回列表按钮
    private JButton backBtn;

    // 球队列表
    private JLabel clubLable;

    // 球队下拉列表框
    private JComboBox clubBox;

    // 能力值lable
    private JLabel abilityLable;

    // 能力值域
    private JTextField abilityFiled;

    // 国家队lable
    private JLabel isNationLable;

    // 国家队下拉列表
    private JComboBox isNationBox;

    // 身高lablel
    private JTextField heightField;

    // 身高域
    private JLabel hegihtLable;

    // 薪水域
    private JTextField salaryFiled;

    // 薪水lable
    private JLabel salaryLable;

    // 惯用脚lable
    private JLabel mostFooterLable;

    // 惯用脚下拉列表
    private JComboBox mostFooterBox;

    // 场上位置lable
    private JLabel postionLabel;
    // 场上位置列表

    private JComboBox positionBox;

    // 姓名文本域
    private JTextField nameTextFiled;


    // 定义分隔符
    private JSeparator jSeparator1;

    public UpdateMemberFrame(FootBallMember footBallMember) {
        this.footBallMember = footBallMember;
        initGUI();
    }

    private void initGUI() {
        try {
            getContentPane().setLayout(null);
            this.setTitle("\u6839\u636e\u59d3\u540d\u4fee\u6539\u7403\u5458\u4fe1\u606f");

            jLabel1 = new JLabel();
            getContentPane().add(jLabel1);
            // 要修改的球员信息
            jLabel1.setText("\u586b\u5199\u8981\u4fee\u6539\u7684\u7403\u5458\u4fe1\u606f");
            jLabel1.setBounds(88, 12, 243, 17);
            jLabel1.setForeground(new java.awt.Color(255, 0, 0));

            jSeparator1 = new JSeparator();
            getContentPane().add(jSeparator1);
            jSeparator1.setBounds(-6, 74, 390, 10);

            // 球员姓名
            nameLable = new JLabel();
            getContentPane().add(nameLable);
            nameLable.setText("\u7403\u5458\u59d3\u540d");
            nameLable.setBounds(33, 84, 55, 17);

            // 姓名域
            nameTextFiled = new JTextField();
			nameTextFiled.setEditable(false);
            getContentPane().add(nameTextFiled);
            nameTextFiled.setBounds(106, 83, 216, 24);

            // 年龄
            ageLabel = new JLabel();
            getContentPane().add(ageLabel);
            ageLabel.setText("\u5e74\u9f84");
            ageLabel.setBounds(33, 117, 55, 17);

            // 年龄域
            ageTextField = new JTextField();
            getContentPane().add(ageTextField);
            //光标离开后，计算薪水值
            ageTextField.addFocusListener(new FocusAdapter() {
                @Override
                public void focusLost(FocusEvent e) {
                    //当年龄计算完毕，需要自动计算出薪水
                    int nowAge = Integer.parseInt(ageTextField.getText());
                    //当前薪水
                    BigDecimal salary = new BigDecimal(nowAge - SalaryBase.BASE_AGE).
                            multiply(new BigDecimal(SalaryBase.BASE_STEP)).
                            multiply(SalaryBase.START_SALARY);
                    DecimalFormat format = new DecimalFormat("0.00");

                    //根据年龄计算所得值
                    salaryFiled.setText(format.format(salary));
                }
            });
            ageTextField.setBounds(106, 113, 216, 24);

            // 惯用脚
            mostFooterLable = new JLabel();
            getContentPane().add(mostFooterLable);
            mostFooterLable.setText("\u60ef\u7528\u811a");
            mostFooterLable.setBounds(33, 153, 55, 17);

            // 惯用脚的下拉列表
            mostFooterBox = new JComboBox();
            // 设置惯用脚下拉列表值
            for (MostFooterEnum mostFooterEnum : MostFooterEnum.values()) {
                // 设置值
                mostFooterBox.addItem(mostFooterEnum.toString());

            }
            getContentPane().add(mostFooterBox);
            mostFooterBox.setBounds(106, 150, 216, 24);

            // 场上位置
            postionLabel = new JLabel();
            getContentPane().add(postionLabel);
            postionLabel.setText("\u573a\u4e0a\u4f4d\u7f6e");
            postionLabel.setBounds(33, 191, 55, 17);

            // 场上位置下拉列表

            positionBox = new JComboBox();
            getContentPane().add(positionBox);
            // 场上位置下拉列表值
            for (Position position : Position.values()) {

                positionBox.addItem(position.toString());

            }
            positionBox.setBounds(106, 184, 216, 24);

            // 薪水
            salaryLable = new JLabel();
            getContentPane().add(salaryLable);
            salaryLable.setText("\u85aa\u6c34");
            salaryLable.setBounds(33, 227, 46, 17);

            // 薪水文本域
            salaryFiled = new JTextField();
            getContentPane().add(salaryFiled);
			//不可编辑，随年龄的增长计算
			salaryFiled.setEditable(false);

            salaryFiled.setBounds(106, 224, 67, 24);

            {
                // 身高
                hegihtLable = new JLabel();
                getContentPane().add(hegihtLable);
                hegihtLable.setText("\u8eab\u9ad8");
                hegihtLable.setBounds(202, 226, 38, 17);

                // 身高文本域
                heightField = new JTextField();
                getContentPane().add(heightField);
                heightField.setBounds(252, 223, 70, 24);

                // 所属球队
                clubLable = new JLabel();
                getContentPane().add(clubLable);
                clubLable.setText("\u6240\u5c5e\u7403\u961f");
                clubLable.setBounds(33, 266, 59, 17);

                // 球队文本域
                clubBox = new JComboBox();

                List<String> clubs = FootBallClubConstants.getALlFootBallData();
                // 设置球队下拉列表值
                for (String club : clubs) {

                    clubBox.addItem(club);

                }
                getContentPane().add(clubBox);
                clubBox.setBounds(104, 265, 211, 24);

                // 能力值
                abilityLable = new JLabel();
                getContentPane().add(abilityLable);
                abilityLable.setText("\u80fd\u529b\u503c");
                abilityLable.setBounds(33, 305, 59, 17);

                // 能力值文本域
                abilityFiled = new JTextField();
                getContentPane().add(abilityFiled);
                abilityFiled.setBounds(104, 300, 211, 24);

                // 是否国家队成员
                isNationLable = new JLabel();
                getContentPane().add(isNationLable);
                isNationLable.setText("\u56fd\u5bb6\u961f");
                isNationLable.setBounds(33, 352, 59, 17);

                // 国家队文本域
                isNationBox = new JComboBox();
                // 设置国家队下拉列表值
                isNationBox.addItem("是");
                isNationBox.addItem("否");
                getContentPane().add(isNationBox);
                isNationBox.setBounds(104, 352, 211, 24);

                okBtn = new JButton();
                getContentPane().add(okBtn);
                okBtn.setText("\u786e\u5b9a");
                okBtn.setBounds(92, 400, 79, 24);
                okBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        okBtnActionPerformed(evt);
                    }
                });

                backBtn = new JButton();
                getContentPane().add(backBtn);
                backBtn.setText("\u8fd4\u56de");
                backBtn.setBounds(219, 400, 84, 24);
                backBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        backBtnActionPerformed();
                    }
                });
            }
            // 开始设置显示值
            // 姓名
            nameTextFiled.setText(footBallMember.getName());
            // 年龄
            ageTextField.setText(footBallMember.getAge() + "");
            // 惯用脚
            mostFooterBox.setSelectedItem(footBallMember.getMostFooterEnum().toString());
            // 位置
            positionBox.setSelectedItem(footBallMember.getPosition().toString());
            // 薪水
            salaryFiled.setText(footBallMember.getSalary().toPlainString());
            // 球队
            clubBox.setSelectedItem(footBallMember.getClub());
            // 身高
            heightField.setText(footBallMember.getHeight());
            // 能力值
            abilityFiled.setText(footBallMember.getAbilityValue() + "");
            // 国家队
            isNationBox.setSelectedItem(footBallMember.isNation() ? "是" : "否");

            this.setSize(400, 600);
            // super.center();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	/**
	 * 当点击修改按钮后 做文本域的信息收集
	 * @param evt
	 */
    private void okBtnActionPerformed(ActionEvent evt) {
		//姓名
		String name = nameTextFiled.getText();
		//年龄
		int age = Integer.parseInt(ageTextField.getText());
		//惯用脚
		String mostFooterString = mostFooterBox.getSelectedItem().toString();
		MostFooterEnum mostFooterEnum = null;
		for (MostFooterEnum mostFooterEnum1 : MostFooterEnum.values()){

			if (mostFooterEnum1.toString().equals(mostFooterString)){
				mostFooterEnum = mostFooterEnum1;
			}

		}
		if (mostFooterEnum == null){
			//默认右脚
			mostFooterEnum = MostFooterEnum.RIGHT_FOOTER;
		}
		//场上位置
		String positionString = positionBox.getSelectedItem().toString();
		Position position = null;
		for (Position position1 : Position.values()){

			if (position1.toString().equals(positionString)){
				position = position1;
			}

		}
		if (position == null){
			//默认位置
			position = Position.GUARD;

		}
		//年薪
		BigDecimal salary = new BigDecimal(salaryFiled.getText());
		//所在球队
		String club = clubBox.getSelectedItem().toString();
		//球员身高
		String height = heightField.getText();
		//能力值
		int ability = Integer.parseInt(abilityFiled.getText());
		//国家队成员
		boolean isNation = false;
		String isNationString = isNationBox.getSelectedItem().toString();
		if (isNationString.equals("是")){
			isNation = true;
		}

        if (name == null || name.equals("")) {
            JOptionPane.showMessageDialog(this, "球员名字必须填写！");
            nameTextFiled.setFocusable(true);
        }
		//加其他校验规则
        if (age < 16){
            JOptionPane.showMessageDialog(this,"该年龄段还不能进入职业，请重新填写!");
            ageTextField.setFocusable(true);
            return;
        }else if (age > 35){
            JOptionPane.showMessageDialog(this,"改年龄段已经退役，请重新填写!");
            ageTextField.setFocusable(true);
            return;
        }
        if (position == Position.GOALKEEPER){
            //如果是门将，必须满足身高
            if (Integer.parseInt(height) < 185){
                JOptionPane.showMessageDialog(this,"185cm以上才适合做门将");
                heightField.setFocusable(true);
                return;
            }
        }
        if (isNation){
            //如果是国家队，需要满足能力值90以上
            if (ability < 90){
                JOptionPane.showMessageDialog(this,"能力值在90以上才能入选国家队!");
                ageTextField.setFocusable(true);
                return;
            }

        }

		//当前修改的member
		FootBallMember currentMember = new FootBallMember(
				age,
				mostFooterEnum,
				position,
				salary,
				club,
				name,
				height,
				ability,
				isNation);
		List<FootBallMember> members = FootBallMemberConstants.getFootBallMembers();
		for (FootBallMember member : members){
			//找到当前球员，修改其数据
			if (member.equals(currentMember)){
				member.setAge(currentMember.getAge());
				member.setMostFooterEnum(currentMember.getMostFooterEnum());
				member.setPosition(currentMember.getPosition());
				member.setSalary(currentMember.getSalary());
				member.setClub(currentMember.getClub());
				member.setName(currentMember.getName());
				member.setHeight(currentMember.getHeight());
				member.setAbilityValue(currentMember.getAbilityValue());
				member.setNation(currentMember.isNation());
			}

		}


        if (JOptionPane.showConfirmDialog(this, "确定要修改这个球员信息吗？", "询问", JOptionPane.YES_NO_OPTION)
				== JOptionPane.YES_OPTION){
			//更新数据
			FootBallMemberConstants.setFootBallMembers(members);
		}

    }

    protected void backBtnActionPerformed() {
        this.setVisible(false);
        this.dispose();
		//显示列表界面
		DispMemberFrame dispMemberFrame = new DispMemberFrame();
		dispMemberFrame.setVisible(true);

    }

    public static Object getGUIBuilderInstance() {
        return new UpdateMemberFrame(Boolean.FALSE);
    }

    public UpdateMemberFrame(Boolean initGUI) {
        super();
    }

}
